package com.rcp.manager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



@WebServlet("/DailyOrders")
public class DailyOrders extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		// Define connection variables
		String className = "com.mysql.jdbc.Driver";
		String url		 = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
		String dbusername  = "cs437group01";
		String dbpassword  = "VHj#dj!v";
		ArrayList<String> dailyOrders = new ArrayList<String>();
		String date = "";
		String Name = "";
		String timeStamp = "";
		String dateTimeStamp = "";
		
		try {
			// Include the driver class
			Class.forName( className );

			// Establish a connection to the database
			Connection connection =
					DriverManager.getConnection(url, dbusername, dbpassword);
			
			//get date right now
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
			String dateWithTime = sdf.format(cal.getTime());
			date = dateWithTime.substring(0, 10);

			String query = "select e.Name, e.Cost, o.Timestamp  From Entrees e INNER JOIN OrderToEntrees ote ON e.EntreeID = ote.EntreeID JOIN Orders o ON ote.OrderID = o.OrderID";

			// Get a reference to the statement object that will execute our query
			// on the server
			Statement statement = connection.createStatement();

			// Execute the query and Store the returned records (ResultSet)
			ResultSet resultSet = statement.executeQuery(query);



			while ( resultSet.next() ) 
			{
				Name = resultSet.getString("Name");
				timeStamp = resultSet.getString("timeStamp");
				dateTimeStamp = timeStamp.substring(0, 10);
				dailyOrders.add(Name);
				dailyOrders.add(dateTimeStamp);
			}

			statement.executeQuery(query);	

			// Close our DB Connection
			connection.close();

		}
		catch(SQLException e){		
			e.printStackTrace();
		}
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}

		response.setContentType("text/html");

		PrintWriter out = response.getWriter();

		out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
		out.println("<head>");
		out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />");
		out.println("<title>Daily Orders</title>");
		out.println("<link rel='stylesheet' type='text/css' href='css/homework1.css' />");
		out.println("<script type='text/javascript' src='js/jquery-1.4.2.js'></script>");
		out.println("<script type='text/javascript' src='js/homework1.js'></script>");
		out.println("</head>");
		out.println("<body>");

		out.println("<div id='header'>");
		out.println("<h1>RCP</h1>");
		out.println("</div>");
		out.println("<p> ");
		out.println("Daily Orders for " + date + "<br /><br />");
		
		//name is even timestamp is odd
		for(int i = 0; i < dailyOrders.size();i++)
		{
			if(i % 2 == 1)
			{
				if(dailyOrders.get(i).equals(date))
				{
					out.println(dailyOrders.get(i) + " " + dailyOrders.get(i-1) + "<br/>");
				}
			}
		}
		out.println("</p>");
		out.println("</div>");
		out.println("</body>");
		out.println("</html>");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
